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,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0], [0,1,0,0,0,0,0,1,0,0,1,0,0,0,1,0]);
var obj = Array("стена.png");
var elem_map = Array("вода.png","земля.png","болото.png");
var gamer = Array("игрок.png");
var gamer_img = new Array();
var elem_img = new Array();
var obj_img = new Array();
var loaded = 0;
var load_time;
var y_mouse;
var x_mouse;
var x_map = 375;
var y_map = 0;
var x_player = 15;  // координата X фигуры
var y_player = 15;  // координата Y фигуры
var x_save, y_save; // координаты для сохранения позиции фигуры
var arc11 = 0, arc12 = 0, arc21 = 0, arc22 = 0, arc31 = 0, arc32 = 0;
var focus = true;
var x = 0, y = 0;

function load_Image() {
    // загрузка изображений
    for (var i = 0; i < elem_map.length; i++) {
        elem_img[i] = new Image();
        elem_img[i].src = elem_map[i];
        elem_img[i].onload = function () {
            loaded++;
        }
    }
    for (var i = 0; i < gamer.length; i++) {
        gamer_img[i] = new Image();
        gamer_img[i].src = gamer[i];
        gamer_img[i].onload = function () {
            loaded++;
        }
    }
    for (var i = 0; i < obj.length; i++) {
        obj_img[i] = new Image();
        obj_img[i].src = obj[i];
        obj_img[i].onload = function () {
            loaded++;
        }
    }
}

function load_All() {
    if (loaded == elem_map.length + gamer.length + obj.length) {
        clearInterval(load_time);
        load_time = setInterval(game_Update, 50);
        setInterval(DrawElips, 40);
    }
}


function game_Update() {
    ctx.clearRect(0, 0, 800, 400);
    draw_Game();
}


function mouse_Click(e) {
    x = e.pageX;
    y = e.pageY;
    y_mouse = (2 * (y - canvas.offsetTop - y_map) - x + canvas.offsetLeft + x_map) / 2;
    x_mouse = x + y_mouse - x_map - 25 - canvas.offsetLeft
    y_mouse = Math.round(y_mouse / 25);
    x_mouse = Math.round(x_mouse / 25);
    document.title = "tileY:" + y_mouse + " | tileX:" + x_mouse;
}


function draw_Game() {
    var gamer_height = 25;
    var gamer_width = 50;
    for (i = 0; i < map.length; i++) {
        for (j = 0; j < map[i].length; j++) {
            var draw_cell = map[i][j];
            var draw_wall = obj_map[i][j];
            var x_pos = (i - j) * gamer_height + x_map;
            var y_pos = (i + j) * gamer_height / 2 + y_map;
            ctx.drawImage(elem_img[draw_cell], x_pos, y_pos);
            if (draw_wall) {
                ctx.drawImage(obj_img[draw_wall - 1], x_pos, y_pos - (obj_img[draw_wall - 1].height / 2));
            }
            if (i == x_player && j == y_player) {
                // прорисовка фигуры
                x_save = x_pos/4;
                y_save = y_pos;
                ctx.drawImage(gamer_img[0], x_pos, y_pos - (gamer_img[0].height / 2) - 13);
                //ctx.globalCompositeOperation = "destination-over";
            }
            if (i == x_mouse && j == y_mouse) {
                // засветление клетки на месте установки курсора мыши
                if (!draw_wall)
                    ctx.fillStyle = 'rgba(255, 255, 120, 0.7)';
                else
                    ctx.fillStyle = 'rgba(152, 120, 237, 0.7)';
                ctx.beginPath();
                //ctx.moveTo(x_save * 4 +  25, y_save + 12.5);
                //ctx.lineTo(x + 25, y + 12.5);
                //ctx.stroke();
                ctx.moveTo(x_pos, y_pos + 12.5);
                ctx.lineTo(x_pos + 25, y_pos);
                ctx.lineTo(x_pos + 50, y_pos + 12.5);
                ctx.lineTo(x_pos + 25, y_pos + 25);
                ctx.fill();
            }
        }
    }
    if(focus) {
        ctx.save();
        ctx.strokeStyle = "rgba(217, 220, 95, 0.5)";
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.scale(4, 1);
        ctx.arc(x_save + 6.25, y_save + 12.5, 6.25, 0, Math.PI * 2, true);
        ctx.stroke();
        ctx.restore();
    }
}



function Key_code(event) {
    var keyCode;
    if(focus) {
    if (event == null) {
        keyCode = window.event.keyCode;
    }
    else {
        keyCode = event.keyCode;
    }
    document.title = keyCode;
    switch (keyCode) {
        case 38:
            if (!obj_map[x_player - 1][y_player]) {
                x_player--;
            }
            break;
        case 40:
            if (!obj_map[x_player + 1][y_player]) {
                x_player++;
            }
            break;
        case 39:
            if (!obj_map[x_player][y_player - 1] && y_player > 0) {
                y_player--;
            }
            break;
        case 37:
            if (!obj_map[x_player][y_player + 1] && y_player < obj_map[0].length - 1) {
                y_player++;
            }
            break;
        default:
            break;
    }
    }
}


function mouseDoubleDown(evt) {
    if (!obj_map[x_mouse][y_mouse]) {
        // проверка на стену
        x_player = x_mouse;
        y_player = y_mouse;
        focus = true;
    }
}


function mouseDown(evt) {
    if(x_player != x_mouse || y_player != y_mouse) {
        focus = false;
    }
    else if(x_player == x_mouse && y_player == y_mouse) {
        focus = true;
    }
}


function DrawElips() {
    if(focus) {
        if(arc11 >= 360) {
            arc11 = 0;
        }
        arc12 = arc11 + 90;
        ctx.save();
        ctx.strokeStyle = "rgba(255, 0, 0, 1)";
        ctx.lineWidth = 2;
        ctx.beginPath();
        ctx.scale(4, 1);
        ctx.arc(x_save + 6.25, y_save + 12.5, 6.25, (arc11 * Math.PI)/180, (arc12 * Math.PI)/180, false);
        ctx.stroke();
        ctx.restore();
        arc11 += 30;
    }
}


function init(){
	document.onkeydown = Key_code;  // обработчик нажатия на клавиши
	canvas = document.getElementById("canvas")
	ctx = canvas.getContext("2d");	    
	load_Image();   // загрузка изображений
	load_time = setInterval(load_All, 100);
	canvas.addEventListener("mousemove", mouse_Click, false);   // обработчик кликов мыши
    canvas.ondblclick = mouseDoubleDown;
    canvas.onmousedown = mouseDown;
}